/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DTO.KHACHHANGDTO;
import DAO.KHACHHANGDAO;

/**
 *
 * @author Mr_Quay
 */
@WebServlet(name = "themkhachhang", urlPatterns = {"/themkhachhang"})
public class themkhachhang extends HttpServlet {
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. * */ 
            KHACHHANGDAO dao = new KHACHHANGDAO();
            KHACHHANGDTO dto = new KHACHHANGDTO();
            

            dto.setTenkh((String) request.getParameter("textTenKH"));
            dto.setDiachi((String) request.getParameter("textDiaChi"));
            dto.setPhone((String) request.getParameter("textSDT"));

            
            boolean kq = dao.themkhachhang(dto);
            String temp;
            if(kq==true)
            {
                temp = "Thêm Khách Hàng Thành Công";
                
            }
            else
            {
                temp = "Lỗi!!!!!!!!!!!";
            }
            request.setAttribute("ketqua", temp);
            
            request.getRequestDispatcher("/thongbao.jsp").forward(request, response);
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally {            
            out.close();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
     @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
